package com.alen.sun.service.Impl;

import com.alen.sun.bean.Admin;
import com.alen.sun.bean.Folk;
import com.alen.sun.bean.PageBean;
import com.alen.sun.dao.AdminMapper;
import com.alen.sun.service.AdminService;
import com.alen.sun.util.PhoneCode;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
@Service
@Transactional
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    /**
     * 添加管理员
     * @param admin
     * @return
     */
    @Override
    public Boolean addAdmin(Admin admin) {
        if(admin.getName()!=null && !admin.getName().isEmpty()
            && admin.getPassword() !=null && !admin.getPassword().isEmpty()
            && admin.getPhoneNum() !=null && !admin.getPhoneNum().isEmpty()){
            //添加之前先检查是否存在
            Integer count = adminMapper.selectByPhoneNum(admin);
            if(count == null || count == 0) {
                //账号不存在
                PhoneCode phoneCode = new PhoneCode();
                //生成adminId
                while (true){
                    String adminId = phoneCode.generateCode();
                    //查询adminId是否重复,否则循环生成,直到不重复
                    Integer count1 = adminMapper.selectByAdminId(adminId);
                    if(count1 == null || count1 == 0){
                        //没有查询到一样的id
                        admin.setAdminId(adminId);
                        //跳出循环
                        break;
                    }
                }
                //密码加密
                String encryptedPassword = DigestUtils.md5Hex(admin.getPassword());
                if (encryptedPassword != null) {
                    admin.setPassword(encryptedPassword);
                } else {
                    // 加密失败处理
                    return false;
                }
                //执行添加记录操作
                Boolean flag = adminMapper.addAdmin(admin);
                return flag;
            }
        }
        return false;
    }

    //管理员登录

    public Boolean login(Admin admin) {
        if(admin.getPhoneNum()!=null && !admin.getPhoneNum().isEmpty()
            && admin.getPassword()!=null && !admin.getPassword().isEmpty()){
            //账号密码都不为空
            //数据库查找记录
            Admin admin1 = adminMapper.selectPhoneNum(admin);
            if(admin1==null){
                log.info("管理员账号不存在");
            }else if(DigestUtils.md5Hex(admin.getPassword()).equals(admin1.getPassword())){
                log.info("登录成功");
                return true;
            }else {
                log.info("账号或密码错误");
                return false;
            }
        }
        return false;
    }

    /**
     * 修改管理员信息
     * @param admin
     * @return
     */
    @Override
    public Boolean updateAdmin(Admin admin) {
        //判断接受的参数是否全不为空
        if(admin.getName()!=null && !admin.getName().isEmpty()
                && admin.getPassword() !=null && !admin.getPassword().isEmpty()
                && admin.getPhoneNum() !=null && !admin.getPhoneNum().isEmpty()
                && admin.getAdminId() !=null && !admin.getAdminId().isEmpty()){
            //先加密管理员的密码
            String encryptedPassword = DigestUtils.md5Hex(admin.getPassword());
            if (encryptedPassword != null) {
                admin.setPassword(encryptedPassword);
            } else {
                // 加密失败处理
                return false;
            }
            Boolean flag = adminMapper.updateAdmin(admin);
            return flag;
        }
        return false;
    }

    /**
     * 删除管理员
     * @param admin
     * @return
     */
    @Override
    public Boolean deleteAdmin(Admin admin) {
        Boolean flag = adminMapper.deleteAdmin(admin);
        return flag;
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageBean page(Integer page, Integer pageSize) {
        //1. 设置分页参数
        //计算参数start
        Integer start = (page-1)*pageSize;
        List<Admin> adminList = adminMapper.page(start, pageSize);
        //2.查询总数
        Long count = adminMapper.count();
        PageBean pageBean = new PageBean(count,adminList);
        return pageBean;
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public Boolean deleteByIds(List<String> ids) {
        Boolean flag = adminMapper.deleteByIds(ids);
        return flag;
    }
}
